<?php

namespace Api\Member\AuthManager\Self;

use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * 私有权限控制器
 */
class Controller extends BaseController
{
    /**
     * 构造方法
     */
    public function __construct(
        ManagerRepository $ManagerRepository
    ) {
        $this->ManagerRepository = $ManagerRepository;
    }

    /**
     * 获取当前账号信息
     * 
     * @return void
     */
    public function getSelf()
    {
        $manager = $this->ManagerRepository->getSelf();
        _success($manager);
    }

    /**
     * 修改当前账号登录密码
     * 
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function changePassword(Request $request)
    {
        try {
            $oldPassword = $request->get('old');
            $nowPassword = $request->get('now');

            $this->ManagerRepository->changePassword($oldPassword, $nowPassword);
            _success();
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }

    /**
     * 管理员列表
     * @param \Illuminate\Http\Request $request
     */
    public function managerList(Request $request)
    {
        $needColumns = [
            'id',
            'account',
            'country_code',
            'mobile',
            'last_ip',
            'status',
            'roles',
            'auth_fail_num',
            'confine_time',
            'created_at',
            'updated_at'
        ];
        $where = [['status', '<>', 3]];
        $needPerPage = $request->get('size');

        $list = $this->ManagerRepository->pageList($needColumns, $where, $needPerPage);
        _success($list);
    }

    /**
     * 添加管理员提交
     * @param \Illuminate\Http\Request $request
     */
    public function managerAdd(Request $request)
    {
        try {
            $data['account']           = $request->get('account');
            $data['country_code']      = $request->get('country_code');
            $data['mobile']            = $request->get('mobile');
            $data['status']            = $request->get('status');
            $roles                     = $request->get('roles');
            $data['roles']             = (isset($roles) && is_array($roles)) ? $roles : [];
            $this->ManagerRepository->managerAdd($data);
            _success();
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }

    /**
     * 编辑管理员
     * @param \Illuminate\Http\Request $request
     */
    public function managerEdit(Request $request, $managerId)
    {
        try {
            $data['account']           = $request->get('account');
            $data['country_code']      = $request->get('country_code');
            $data['mobile']            = $request->get('mobile');
            $data['status']            = $request->get('status');
            $roles                     = $request->get('roles');
            $data['roles']             = (isset($roles) && is_array($roles)) ? $roles : [];
            $this->ManagerRepository->managerEdit($managerId, $data);
            _success();
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }

    /**
     * 删除指定
     * 
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function managerDelete(Request $request, $managerId)
    {
        try {
            $this->ManagerRepository->managerDelete($managerId);
            _success();
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }


}
